package cn.edu.nuc.ehelper;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nuc.dictProceed.BackWords;
import cn.edu.nuc.wordsbook.Constant;
import cn.edu.nuc.wordsbook.SQLiteOperationHelper;

public class QueryResultActivity extends Activity {
	private String english = "";
	private String chinese = "";
	private Button addWords;
	Cursor backResult = null;

	public String getEnglish() {
		return english;
	}

	public String getChinese() {
		return chinese;
	}

	public Cursor getBackResult() {
		return backResult;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queryresult);
		addWords = (Button) findViewById(R.id.addToWordsBookButtonId);
		addWords.setText("加入单词本");
		
		Intent intentpre = getIntent();
		String abc = intentpre.getStringExtra("words");

		BackWords b = new BackWords(this);
		System.out.println(abc);
		backResult = b.lookup(abc);
		backResult.moveToFirst();
		System.out.println(backResult);
		
		if (backResult.getCount() == 0) {
			addWords.setEnabled(false);
			english = "查无此词";
			chinese = "null";
		} else {
			addWords.setEnabled(true);
			english = backResult.getString(0);
			chinese = backResult.getString(1);
		}
		//
		// System.out.println("英："+backResult.getString(0));
		// System.out.println("意："+backResult.getString(1));
		TextView t = (TextView) findViewById(R.id.queryResultViewId);
		t.setText(english + "\n" + chinese);

		addWords.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				ContentValues values = new ContentValues();
				long millDate = System.currentTimeMillis();
				String date = DateFormat.format("yyyy-MM-dd", millDate)
						.toString();
				values.put("english", english);
				values.put("translation", chinese);
				values.put("lastdate", date);
				values.put("millDate", millDate);
				SQLiteOperationHelper dbHelper = new SQLiteOperationHelper(
						QueryResultActivity.this, Constant.TABLEName, null, 1);
				long flag = dbHelper.insert(values);
				if (flag == -1) {
					Toast.makeText(QueryResultActivity.this, "单词已存在，添加失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

}
